<?php

class cntrlshares implements viewController {

	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "shares.js"), "CSS" => array("facebox.css") );
	private $pageName = "PicBoard Shares";
	private $userObj;
	private $page;
	
	public function __construct(user &$userObj){
		if( $userObj->getAccessLevel() == -1 || $userObj->getAccessLevel() == "" ){
			// User has to be a global admin to access this view
			header("Location: home");
			exit;
		}
		
		$this->userObj = $userObj;
		
		parse_str( $_GET['act'] );
	
		$this->generateSidebar();
		$this->generateArea();
	
		$this->page = $p;
	
		if($p == "outbox"){
			$data = $this->userObj->getOutbox();
			$blurb = "<var id=\"lang_147_outboxBlurb\"></var>";
			$buttonText = "<var id=\"lang_209_unShare\"></var>";
		} else {
			$data = $this->userObj->getInbox();
			$blurb = "<var id=\"lang_146_inboxBlurb\"></var>";
			$buttonText = "<var id=\"lang_208_deleteSelectedMessages\"></var>";
		}
	
		$charts = $this->formatChartsAndReturn(new template('fragment.selectableListDisplayItem'), $data);
	
		$this->area->replace("content", $charts);
		
		$this->side->replace("blurb", $blurb);
		$this->side->replace("buttonText", $buttonText);
	
	}

	private function generateSidebar(){
		$this->side = new template('control.shareSide');
	}
	
	private function generateArea(){
		$this->area = new template('area.shares');
	}

	private function formatChartsAndReturn(template &$itemTemplate, $inputData){
		$chartOutput = "";
		
		$i=1;
		
		foreach($inputData as $chart){
						
			$out = "<a href='chart?id=" . $chart['chID'] . "'>" . $chart['name'] . "</a>";
			$chart['description'] = ($chart['description'] != "") ? "<p>" . $chart['description'] . "</p>" : "<p class='smgreyText'><var id=\"lang_201_noDesc\"></var></p>"; 
			
			$out .= $chart['description'] . "<br /><var id=\"lang_145_sharedBy\"></var>: " . $chart['firstName'] . " " . $chart['lastName'];
			$out .= "<input type='hidden' name='ouID' value='" . ($this->page == "inbox" ? $chart['uID_from'] : $chart['uID_to']) . "'>";

			$imageURL = SYS_BASEURL . "/images/document.png";
			
			// Is this IE & SVG?
			
			$imageTemplate = sprintf("<img src='%s' height='70' width='70' />", $imageURL);
			
			$itemTemplate->replace("desc", $out);
			$itemTemplate->replace("i", $i);
			$itemTemplate->replace("image", $imageTemplate);
			$chartOutput .= $itemTemplate->get();
			$itemTemplate->reset();
			$i++;
		}	
		return $chartOutput;
		
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return "";
	}

}

?>